<?php
$config_file = parse_ini_file("../../properties/config.ini");
$include_path = "../";
include_once("../util/db_manager.php");
include_once("../util/log_writer.php");
$lw = LogWriter::getInstance("../../properties/log4php-level2.properties");
$error = false;
$msg = "";
try {
	$artifact_id = $_POST["artifactId"];
	$artifact_type = $_POST["artifactType"];
	//start db transaction
	$lw->debug("getting db manager");
	$dbManager = DBManager::getInstance($config_file);
	$lw->debug("getting connection");
	$conn = $dbManager->fetchConnection();
	
	if ($_POST["update"] == "Y") {
		$lw->debug("update visitor count");
		$query = "UPDATE artifacts SET visitor_count=visitor_count+1 WHERE artifact_id=" . $artifact_id; 
		$lw->debug(" firing " . $query);
		$result = $dbManager->processQuery($query,$conn);
	}
	$lw->debug("getting artifact visitor count");
	$query = "SELECT visitor_count FROM artifacts WHERE artifact_id=" . $artifact_id;
	$lw->debug(" firing " . $query);
	$result = $dbManager->processQuery($query,$conn);
	$lw->debug(" getting total ");
	while ($row = mysql_fetch_array($result)) {
		$visitor_count = $row["visitor_count"];
	}
	$lw->debug("getting artifact comment count");
	$query = "SELECT count(1) 'total' FROM comments WHERE artifact_id=" . $artifact_id . " and artifact_type='" . $artifact_type . "'";
	$lw->debug(" firing " . $query);
	$result = $dbManager->processQuery($query,$conn);
	$lw->debug(" getting total ");
	while ($row = mysql_fetch_array($result)) {
		$comment_count = $row["total"];
	}
	$lw->debug("closing connection");
	$dbManager->releaseConnection($conn);
	$lw->debug("done");
} catch (Exception $e) {
	$lw->error($e);
	$error = true;
	$msg = $e->getMessage();
} 
	include("artifact_stat_message_parser.php");
?>
